package com.server.jaxrs;


import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import java.util.List;
import java.util.UUID;

import com.server.entity.Account;
import com.server.entity.AccountType;
import com.server.entity.User;

@Stateless
@Path("Account")
public class AccountAPI {	
    @SuppressWarnings("unused")
    @Context
    private UriInfo context;

    
	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public AccountAPI() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Retrieves representation of an instance of AccountAPI
     * @return an instance of String
     */
    @SuppressWarnings("unchecked")
	@GET
    @Produces({MediaType.APPLICATION_JSON})
    public List<AccountType> getXml() {
        // TODO return proper representation object
    	Query query= em.createNamedQuery("GetAllUsers");
    	
    	User user=(User )query.getResultList().get(0);
    	
    	query= em.createNamedQuery("GetAccountByUser");
    	query.setParameter("userId", user.getUserId());
    	Account ac= (Account) query.getResultList().get(0);
    	
    	query= em.createNamedQuery("GetAllAccountType");
    	//query.setParameter("userId", user.getUserId());
    	AccountType act= (AccountType) query.getResultList().get(0);
    	ac.setAccountType(act);
    	em.persist(ac);
    	em.flush();
    	
        query= em.createNamedQuery("GetAccountTypeByUser");
        query.setParameter("userId", user.getUserId());
        List<AccountType> at=query.getResultList(); 
        
        return at;
    }

    /**
     * PUT method for updating or creating an instance of AccountAPI
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

}